<?php

namespace App\Library\Logic;

use App\Library\Requests\Coupon\CouponPageRequest;
use App\Models\CouponModel;
use Illuminate\Database\Eloquent\Builder;

class CouponLogic extends CouponModel
{
    /**
     * 优惠券领取列表
     * @param CouponPageRequest $request
     * @return array
     */
    public function paging(CouponPageRequest $request): array
    {
        $builder = $this->newQuery()->where([
            'status' => 1,
            'is_system' => 0,
        ])->where(function (Builder $query) {
            $query->whereNull('start_at')->orWhere('start_at', '<', now()->toDateTimeString());
        })->where(function (Builder $query) {
            $query->whereNull('end_at')->orWhere('end_at', '>', now()->toDateTimeString());
        })->orderBy('sort')->orderByDesc('created_at');
        return paginate(helpBuilder($builder, $request)->paginate($request->limit, [
            'serial_number',
            'title',
            'amount',
            'full_amount',
            'start_at',
            'end_at',
            'quantity',
            'get_count',
            'receive_count',
            'use_count',
        ]));
    }

    /**
     * 优惠券领取列表
     * @param CouponPageRequest $request
     * @return array
     */
    public function claimedPaging(CouponPageRequest $request): array
    {
        $builder = $this->newQuery()->where([
            'status' => 1,
            'is_system' => 1,
        ])->where(function (Builder $query) {
            $query->whereNull('start_at')->orWhere('start_at', '<', now()->toDateTimeString());
        })->where(function (Builder $query) {
            $query->whereNull('end_at')->orWhere('end_at', '>', now()->toDateTimeString());
        })->orderBy('sort')->orderByDesc('created_at');
        return paginate(helpBuilder($builder, $request)->paginate($request->limit, [
            'serial_number',
            'title',
            'amount',
            'full_amount',
            'start_at',
            'end_at',
            'quantity',
            'get_count',
            'receive_count',
            'use_count',
        ]));
    }
}
